<script setup>
    import userApi from "@/api/user/user.js";
    import {ref, reactive} from "vue";
    import {ElMessage, ElMessageBox} from "element-plus";

    //表格数据
    const list = ref([])
    const total = ref(0)
    //分页信息和搜索条件
    const userQuery = reactive({
        page: 1,
        limit: 10
    })

    const loadData = () => {
        userApi.list(userQuery).then(result => {
            list.value = result.data.list
            total.value = result.data.total
        })
    }
    loadData()

    const onSearch = () => {
        userQuery.page = 1;
        loadData();
    }

    //修改status状态
    const handleSwitchChange = (row) => {
        userApi.updateStatus(row.id, row.status).then(result => {
            if (result.code === 0) {
                ElMessage({message: result.msg, type: 'success',})
                dialogFormVisible.value = false
            } else {
                ElMessage.error(result.msg)
            }
        })
    }
</script>

<template>
    <el-card class="">
        <template #header>用户列表</template>

        <el-form :inline="true">
            <el-form-item label="姓名">
                <el-input v-model="userQuery.name" placeholder="请输入姓名" clearable />
            </el-form-item>
            <el-form-item label="手机号">
                <el-input v-model="userQuery.phone" placeholder="请输入手机号" clearable />
            </el-form-item>
            <el-form-item label="创建时间">
                <el-date-picker
                    v-model="userQuery.beginCreateTime"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    placeholder="开始时间"
                />
                <el-date-picker
                    v-model="userQuery.endCreateTime"
                    type="datetime"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    placeholder="结束时间"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSearch">搜索</el-button>
            </el-form-item>
        </el-form>

        <el-table :data="list" style="width: 100%" ref="multipleTableRef">
            <el-table-column type="selection" width="55" />
            <el-table-column fixed prop="id" label="ID" width="150" />
            <el-table-column prop="name" label="姓名"/>
            <el-table-column prop="nickName" label="昵称"/>
            <!--<el-table-column prop="image" label="头像">
                <template #default="scope">
                    <img :src="scope.row.image" style="max-height: 40px;max-width: 120px" />
                </template>
            </el-table-column>-->
            <el-table-column prop="email" label="邮箱"/>
            <el-table-column prop="phone" label="电话"/>
            <el-table-column prop="status" label="状态">
                <template #default="scope">
                    <el-switch
                        v-model="scope.row.status"
                        style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                        inline-prompt
                        active-text="正常"
                        inactive-text="停用"
                        :active-value="1"
                        :inactive-value="0"
                        @change="handleSwitchChange(scope.row)"
                    />
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间"/>
            <el-table-column prop="updateTime" label="更新时间"/>
        </el-table>
        <el-pagination
            v-model:current-page="userQuery.page"
            v-model:page-size="userQuery.limit"
            :page-sizes="[10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @change="loadData"
            style="margin-top: 20px; justify-content: flex-end"/>

    </el-card>
</template>

<style scoped>
    .avatar-uploader .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
    .avatar-uploader .el-upload {
        border: 1px dashed var(--el-border-color);
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
        transition: var(--el-transition-duration-fast);
    }

    .avatar-uploader .el-upload:hover {
        border-color: var(--el-color-primary);
    }
</style>